
define(['ionic', 'services'], function () {
    angular.module('U9Mobile.services')
        .factory('ConnectService', ['$q', '$http', 'FileStorage', 'UtilService', 'AppConstants',
            function ($q, $http, FileStorage, UtilService, AppConstants) {
                var connectInfo = {},
                    companyList = null;
                FileStorage.loadingPromise.then(function () {
                    connectInfo = {
                        address: FileStorage.get('address', AppConstants.DEFAULT_ADDRESS),
                        port: FileStorage.get('port', AppConstants.DEFAULT_PORT),
                        company: FileStorage.get('company', '')
                    };
                });

                var o = {};

                o.setConnect = function (address, port, company) {
                    var defer = $q.defer();

                    connectInfo.address = address;
                    connectInfo.port = port;
                    connectInfo.company = company;

                    FileStorage.set(connectInfo).finally(function () {
                        defer.resolve();
                    });

                    return defer.promise;
                };
                o.setConnectParam = function (key, value) {
                    if (angular.isString(key)) {
                        connectInfo[key] = value;
                    } else if (angular.isObject(key)) {
                        angular.extend(connectInfo, key);
                    }
                    return FileStorage.set(key, value);
                };
                o.getConnect = function (key) {
                    return connectInfo.hasOwnProperty(key) ? connectInfo[key] : null;
                };
                o.getAll = function () {
                    return angular.copy(connectInfo);
                };
                o.getCompanyList = function (address, port, force) {
                    var defer = $q.defer();

                    $q.when(
                        force ? queryCompanyList(address, port) : (companyList || queryCompanyList(address, port))
                    ).then(function(list) {
                        defer.resolve(list);
                    }, function () {
                        defer.reject();
                    });

                    return defer.promise;
                };
                o.getCompanyName = function () {
                    var defer = $q.defer();

                    o.getCompanyList(connectInfo.address, connectInfo.port).then(function () {
                        var companyId = connectInfo.company;
                        if (!companyId || companyId === '' || !companyList) {
                            defer.resolve();
                        }
                        var company;
                        for (var i = 0, len = companyList.length; i < len; i++) {
                            if (companyList[i].id === companyId) {
                                company = companyList[i];
                                break;
                            }
                        }
                        if (company) {
                            defer.resolve(company.name);
                        } else {
                            defer.resolve();
                        }
                    }, function () {
                        defer.resolve();
                    });

                    return defer.promise;
                };

                return o;

                function queryCompanyList (address, port) {
                    var defer = $q.defer();

                    var url = AppConstants.GET_COMPANY_URL.format({
                        address: address,
                        port: port
                    });
                    UtilService.showLoading();
                    $http({
                        method: 'get',
                        url: url,
                        timeout: AppConstants.HTTP_TIME_OUT
                    }).success(function (data) {
                        companyList = data.enterprises;
                        defer.resolve(data.enterprises);
                        UtilService.hideLoading();
                    }).error(function (er) {
                        defer.reject();
                        UtilService.hideLoading();
                    });

                    return defer.promise;
                }
            }
        ]);
});